<template>
  <view>
	  <view class="wrap">
		  <view class="u-tabs-box">
			  <u-tabs-swiper activeColor="#f29100" ref="tabs" :list="list" :current="current" @change="change" :is-scroll="false"
			   swiperWidth="750"></u-tabs-swiper>
		  </view>
		  <swiper class="swiper-box" :current="swiperCurrent" @transition="transition" @animationfinish="animationfinish">
		  		<swiper-item class="swiper-item">
		  			<scroll-view scroll-y style="height: 100%;width: 100%;" @scrolltolower="reachBottom">
		  				<view class="page-box">
		  					<view class="order" v-for="(res, index) in orders[0]" :key="res._id">
		  						<view class="top" @click="toDetail(res.order_sn)">
		  							<view class="left">
		  								<u-icon name="home" :size="30" color="rgb(94,94,94)"></u-icon>
		  								<view class="store">{{ res.order_sn }}</view>
		  								<u-icon name="arrow-right" color="rgb(203,203,203)" :size="26"></u-icon>
		  							</view>
		  							<view class="right">{{ statusName[res.status] }}</view>
		  						</view>
		  						<view class="item" v-for="(item, index) in res.items" :key="index" @click="toDetail(res.order_sn)">
		  							<view class="left">
		  								<image :src="imgUrl+item.goods.pic" mode="aspectFill"></image>
		  							</view>
		  							<view class="content">
		  								<view class="title u-line-2">{{ item.goods.name }} </view>
		  								<view class="type">{{ item.title }}</view>
		  								<view class="delivery-time">付款后5天内发货</view>
		  							</view>
		  							<view class="right">
		  								<view class="price">
		  									￥{{ formatPrice(item.price) }}
		  								</view>
		  								<view class="number">x{{ item.count }}</view>
		  							</view>
		  						</view>
		  						<view class="total" @click="toDetail(res.order_sn)">
		  							共{{ totalNum(res.items) }}件商品 合计:
		  							<text class="total-price">
		  								￥{{ formatPrice(res.total_price) }}
		  
		  							</text>
		  						</view>
		  						<view class="bottom">
		  							<view class="more">
		  								<!-- <u-icon name="more-dot-fill" color="rgb(203,203,203)"></u-icon> -->
		  							</view>
		  							<view class="logistics btn" @click="cancel(res.order_sn)">取消订单</view>
		  							<view class="evaluate btn" @click="pay(res.order_sn,totalPrice(res.items)*100)">立即付款</view>
		  						</view>
		  					</view>
		  					<u-loadmore :status="loadStatus[0]" bgColor="#f2f2f2"></u-loadmore>
		  				</view>
		  			</scroll-view>
		  		</swiper-item>
		  
		  		<swiper-item class="swiper-item">
		  			<scroll-view scroll-y style="height: 100%;width: 100%;" @scrolltolower="reachBottom">
		  				<view class="page-box">
		  					<view class="order" v-for="(res, index) in orders[1]" :key="res._id">
		  						<view class="top" @click="toDetail(res.order_sn)">
		  							<view class="left">
		  								<u-icon name="home" :size="30" color="rgb(94,94,94)"></u-icon>
		  								<view class="store">{{ res.order_sn }}</view>
		  								<u-icon name="arrow-right" color="rgb(203,203,203)" :size="26"></u-icon>
		  							</view>
		  							<view class="right">{{ statusName[res.status] }}</view>
		  						</view>
		  						<view class="item" v-for="(item, index) in res.items" :key="index" @click="toDetail(res.order_sn)">
		  							<view class="left">
		  								<image :src="imgUrl+item.goods.pic" mode="aspectFill"></image>
		  							</view>
		  							<view class="content">
		  								<view class="title u-line-2">{{ item.goods.name }} </view>
		  								<view class="type">{{ item.title }}</view>
		  								<view class="delivery-time">付款后5天内发货</view>
		  							</view>
		  							<view class="right">
		  								<view class="price">
		  									￥{{ formatPrice(item.price) }}
		  								</view>
		  								<view class="number">x{{ item.count }}</view>
		  							</view>
		  						</view>
		  						<view class="total" @click="toDetail(res.order_sn)">
		  							共{{ totalNum(res.items) }}件商品 合计:
		  							<text class="total-price">
		  								￥{{ totalPrice(res.items) }}
		  
		  							</text>
		  						</view>
		  						<view class="bottom">
		  							<view class="more">
		  								<!-- <u-icon name="more-dot-fill" color="rgb(203,203,203)"></u-icon> -->
		  							</view>
									<view class="evaluate btn" @click="refund(res.order_sn)">申请退款</view>
		  							<view class="evaluate btn" @click="notify(res.order_sn)">提醒发货</view>
		  						</view>
		  					</view>
		  					<u-loadmore :status="loadStatus[1]" bgColor="#f2f2f2"></u-loadmore>
		  				</view>
		  			</scroll-view>
		  		</swiper-item>
		  		<swiper-item class="swiper-item">
		  			<scroll-view scroll-y style="height: 100%;width: 100%;" @scrolltolower="reachBottom">
		  				<view class="page-box">
		  					<view class="order" v-for="(res, index) in  orders[2]" :key="res._id">
		  						<view class="top" @click="toDetail(res.order_sn)">
		  							<view class="left">
		  								<u-icon name="home" :size="30" color="rgb(94,94,94)"></u-icon>
		  								<view class="store">{{ res.order_sn }}</view>
		  								<u-icon name="arrow-right" color="rgb(203,203,203)" :size="26"></u-icon>
		  							</view>
		  							<view class="right">{{ statusName[res.status] }}</view>
		  						</view>
		  						<view class="item" v-for="(item, index) in res.items" :key="index" @click="toDetail(res.order_sn)">
		  							<view class="left">
		  								<image :src="imgUrl+item.goods.pic" mode="aspectFill"></image>
		  							</view>
		  							<view class="content">
		  								<view class="title u-line-2">{{ item.goods.name }} </view>
		  								<view class="type">{{ item.title }}</view>
		  								<view class="delivery-time">付款后5天内发货</view>
		  							</view>
		  							<view class="right">
		  								<view class="price">
		  									￥{{ formatPrice(item.price) }}
		  								</view>
		  								<view class="number">x{{ item.count }}</view>
		  							</view>
		  						</view>
		  						<view class="total" @click="toDetail(res.order_sn)">
		  							共{{ totalNum(res.items) }}件商品 合计:
		  							<text class="total-price">
		  								￥{{ totalPrice(res.items) }}
		  
		  							</text>
		  						</view>
		  						<view class="bottom">
		  							<view class="more">
		  								<!-- <u-icon name="more-dot-fill" color="rgb(203,203,203)"></u-icon> -->
		  							</view>
		  							<view class="logistics btn" @click="expressInfo(res.order_sn)">查看物流</view>
		  							<view class="evaluate btn" @click="confirm(res.order_sn)">确认收货</view>
		  						</view>
		  					</view>
		  					<u-loadmore :status="loadStatus[2]" bgColor="#f2f2f2"></u-loadmore>
		  				</view>
		  			</scroll-view>
		  		</swiper-item>
		  		<swiper-item class="swiper-item">
		  			<scroll-view scroll-y style="height: 100%;width: 100%;" @scrolltolower="reachBottom">
		  				<view class="page-box">
		  					<view class="order" v-for="(res, index) in  orders[3]" :key="res._id">
		  						<view class="top" @click="toDetail(res.order_sn)">
		  							<view class="left">
		  								<u-icon name="home" :size="30" color="rgb(94,94,94)"></u-icon>
		  								<view class="store">{{ res.order_sn }}</view>
		  								<u-icon name="arrow-right" color="rgb(203,203,203)" :size="26"></u-icon>
		  							</view>
		  							<view class="right">{{ statusName[res.status] }}</view>
		  						</view>
		  						<view class="item" v-for="(item, index) in res.items" :key="index" @click="toDetail(res.order_sn)">
		  							<view class="left">
		  								<image :src="imgUrl+item.goods.pic" mode="aspectFill"></image>
		  							</view>
		  							<view class="content">
		  								<view class="title u-line-2">{{ item.goods.name }} </view>
		  								<view class="type">{{ item.title }}</view>
		  								<view class="delivery-time">已确认收货</view>
		  							</view>
		  							<view class="right">
		  								<view class="price">
		  									￥{{ formatPrice(item.price) }}
		  								</view>
		  								<view class="number">x{{ item.count }}</view>
		  							</view>
		  						</view>
		  						<view class="total" @click="toDetail(res.order_sn)">
		  							共{{ totalNum(res.items) }}件商品 合计:
		  							<text class="total-price">
		  								￥{{ totalPrice(res.items) }}
		  
		  							</text>
		  						</view>
		  						<view class="bottom">
		  							<view class="more">
		  								<u-icon name="more-dot-fill" color="rgb(203,203,203)"  ></u-icon>
		  							</view>
		  							<view class="logistics btn" @click="expressInfo(res.order_sn)">查看物流</view>
		  							<view class="evaluate btn" @click="comment(res.order_sn)">评价</view>
		  						</view>
		  					</view>
		  					<u-loadmore :status="loadStatus[3]" bgColor="#f2f2f2"></u-loadmore>
		  				</view>
		  			</scroll-view>
		  		</swiper-item>
		  	</swiper>
		  </view>
		  
		  <u-modal v-model="showCancel" content="确认取消该订单?" :show-cancel-button="true" @confirm="cancelOrder"></u-modal>
  </view>
</template>

<script>
const orderCloudObject = uniCloud.importObject('orderCloudObject')
const db = uniCloud.database()
const dbOrder = db.collection("t_shop_order")
const dbUser = db.collection("uni-id-users")
const dbOrderItem = db.collection("t_shop_order_item")
const dbOrderLog = db.collection("t_shop_order_log")
const dbGoods = db.collection("t_shop_goods")
const dbCmd = db.command

  export default {
    data() {
      return {
		  showCancel: false,
		  cancelOrderSn: '',
		  imgUrl: 'https://zhy-demo.oss-cn-hangzhou.aliyuncs.com/upload/',
		  orders: [
		  	[],
		  	[],
		  	[],
		  	[]
		  ],
		  listQuery: {
		  	page: 1,
		  	limit: 20
		  },
		  list: [{
			  name: '待付款',
		  }, {
			  name: '待发货',
		  }, {
			  name: '待收货',
		  }, {
			  name: '待评价',
		  }],
		  current: 0,
		  swiperCurrent: 0,
		  tabsHeight: 0,
		  dx: 0,
		  loadStatus: ['loadmore', 'loadmore', 'loadmore', 'loadmore'],
		  statusName: ['待付款', '待发货', '已发货', '已完成', '已取消', '退款中', '已退款', '已评价']
      }
    },
    onReady() {
		//待付款
		this.getOrders(1);
		//待发货
		this.getOrders(2);
		//已发货
		this.getOrders(3);
		//已完成
		this.getOrders(4);
    },
	computed: {
		// 价格小数
		priceDecimal() {
			return val => {
				if (val !== parseInt(val)) {
					return val.slice(-2)
				} else {
					return '00'
				}
			};
		},
		// 价格整数
		priceInt() {
			return val => {
				if (val !== parseInt(val)) return val.split('.')[0]
				else return val
			};
		}
	
	},
    methods: {
		reachBottom() {
			const current = this.current
			if (this.loadStatus[current] == 'nomore') {
				return
			}
			this.listQuery.page++
			this.getOrders(current + 1)
		},
		getOrders(status) {
			this.loadStatus.splice(status - 1, 1, "loading")
			let limit = this.listQuery.limit
			let page = this.listQuery.page
			dbOrder.where({
					   status: dbCmd.eq(status),
					   id_user: db.getCloudEnv('$cloudEnv_uid')
			}).skip((page-1)*limit).limit(limit).get().then(res => {
				console.log(res.result.data.length)
				let orderList = res.result.data
				console.log(orderList)
				if (orderList.length < limit) {
					this.loadStatus.splice(status - 1, 1, "nomore");
				} else {
					this.loadStatus.splice(status - 1, 1, "more");
				}
				for (let i = 0; i < orderList.length; i++) {
					// console.log("status:" + status + " for: " + i)
					orderCloudObject.getUser(orderList[i].id_user).then(res => {
						let user = res.data[0]
						orderList[i].user = user
						orderCloudObject.getOrderItems(orderList[i]._id).then(res => {
							let items = res.data
							for (let j = 0; j < items.length; j++) {
								// console.log("status:" + status + " for: " + i)
								orderCloudObject.getGoodsById(items[j].id_goods).then(res => {
									let goods = res.data[0]
									items[j].goods = goods
									items[j].title = '' + goods.name
									// TODO: getCategoryById
									orderList[i].items = items
									orderList[i].title = '' + orderList[i].create_time
									orderList[i].descript = '' + orderList[i].order_sn
									this.orders[status - 1].push(orderList[i])
								});
							}
						});
					});
				}
			});
		},
		formatPrice(val) {
			return (val / 100).toFixed(2)
		},
		// tab栏切换
		change(index) {
			this.swiperCurrent = index
		},
		// 总价
		totalPrice(item) {
			let price = 0
			item.map(val => {
				price += val.price
			});
			return this.formatPrice(price)
		},
		// 总件数
		totalNum(item) {
			let num = 0;
			if (item) {
				item.map(val => {
					if (val.number) {
						num += val.number
					}
					if (val.count) {
						num += val.count
					}
				});
			}
			return num;
		},
		// tab栏切换
		change(index) {
			this.swiperCurrent = index
		},
		transition({
			detail: {
				dx
			}
		}) {
			this.$refs.tabs.setDx(dx)
		},
		animationfinish({
			detail: {
				current
			}
		}) {
			this.$refs.tabs.setFinishCurrent(current)
			this.swiperCurrent = current
			this.current = current
		},
		toHome() {
			this.$u.route({
				type: 'switchTab',
				url: '/pages/shop/index'
			})
		},
		toDetail(orderSn) {
			this.$u.route({
				url: '/pages/order/detail',
				params: {
					orderSn: orderSn
				}
			})
		},
		cancel(orderNo) {
			this.showCancel = true
			this.cancelOrderSn = orderNo
		},
		cancelOrder() {
			orderCloudObject.cancelOrder(this.cancelOrderSn).then(res => {
				this.getOrders(1)
			})
		},
		expressInfo(orderNo) {
			console.log('查看物流信息', orderNo)
			this.$u.route({
				url: '/pages/order/express',
				params: {
					orderSn: orderNo
				}
			})
		},
		confirm(orderNo) {
			console.log('确认收货', orderNo)
			orderCloudObject.getByOrderSn(orderNo).then(res => {
				let order = res.data[0]
				let descript = "客户确认收货"
				orderCloudObject.saveOrderLog(order._id, descript).then(res => {
					orderCloudObject.updateOrder(order._id, 4).then(res => {
						this.getOrders(3)
					});
				});
			});
		},
		refund(orderSn) {
			console.log('申请退款', orderSn)
			this.$u.toast('已申请退款')
			// 修改状态为退款中
			// dbOrder.where({order_sn: dbCmd.eq(orderSn)})
			// .update({status: 6}).then(res => {
			// 	this.$u.toast('已申请退款')
			// })
		},
		notify(orderSn) {
			console.log('提醒发货', orderSn)
			this.$u.toast('已提醒店铺发货')
		},
		pay(orderNo, totalPrice) {
			this.$u.route({
				url: '/pages/order/payment/payment',
				params: {
					orderSn: orderNo,
					totalPrice: totalPrice
				}
			})
		},
		comment(orderSn) {
			console.log('评论', orderSn)
			// this.$u.toast('敬请期待')
			uni.navigateTo({
				url: "/pages/orderRemark/add",
				params: {
					orderSn: orderSn, 
				}
			})
		}
    }
  }
</script>

<style>
	/* #ifndef H5 */
	page {
		height: 100%;
		background-color: #f2f2f2;
	}

	/* #endif */
</style>

<style lang="scss" scoped>
	.order {
		width: 98%;
		background-color: #ffffff;
		margin: 1%;
		border-radius: 10rpx;
		box-sizing: border-box;
		padding: 20rpx;
		font-size: 28rpx;

		.top {
			display: flex;
			justify-content: space-between;

			.left {
				display: flex;
				align-items: center;

				.store {
					margin: 0 10rpx;
					font-size: 32rpx;
					font-weight: bold;
				}
			}

			.right {
				color: $u-warning-dark;
			}
		}

		.item {
			display: flex;
			margin: 20rpx 0 0;

			.left {
				margin-right: 20rpx;

				image {
					width: 200rpx;
					height: 200rpx;
					border-radius: 10rpx;
				}
			}

			.content {
				flex: 1;
				
				.title {
					font-size: 28rpx;
					line-height: 50rpx;
				}

				.type {
					margin: 10rpx 0;
					font-size: 24rpx;
					color: $u-tips-color;
				}

				.delivery-time {
					color: #e5d001;
					font-size: 24rpx;
				}
			}

			.right {
				margin-left: 10rpx;
				padding-top: 20rpx;
				text-align: right;

				.decimal {
					font-size: 24rpx;
					margin-top: 4rpx;
				}

				.number {
					color: $u-tips-color;
					font-size: 24rpx;
				}
			}
		}

		.total {
			margin-top: 20rpx;
			text-align: right;
			font-size: 24rpx;

			.total-price {
				font-size: 32rpx;
			}
		}

		.bottom {
			display: flex;
			margin-top: 40rpx;
			padding: 0 10rpx;
			justify-content: flex-end;
			align-items: center;

			.btn {
				margin-left: 20rpx;
				line-height: 52rpx;
				width: 160rpx;
				border-radius: 26rpx;
				border: 2rpx solid $u-border-color;
				font-size: 26rpx;
				text-align: center;
				color: $u-info-dark;
			}

			.evaluate {
				color: $u-warning-dark;
				border-color: $u-warning-dark;
			}
		}
	}

	.centre {
		text-align: center;
		margin: 200rpx auto;
		font-size: 32rpx;

		image {
			width: 164rpx;
			height: 164rpx;
			border-radius: 50%;
			margin-bottom: 20rpx;
		}

		.tips {
			font-size: 24rpx;
			color: #999999;
			margin-top: 20rpx;
		}

		.btn {
			margin: 80rpx auto;
			width: 200rpx;
			border-radius: 32rpx;
			line-height: 64rpx;
			color: #ffffff;
			font-size: 26rpx;
			background: linear-gradient(270deg, rgba(249, 116, 90, 1) 0%, rgba(255, 158, 1, 1) 100%);
		}
	}

	.wrap {
		display: flex;
		flex-direction: column;
		height: calc(100vh - var(--window-top));
		width: 100%;
	}

	.swiper-box {
		flex: 1;
	}

	.swiper-item {
		height: 100%;
	}
</style>
